﻿using System;

namespace DotNetImage.Imaging.Filters
{
    /// <summary>
    /// This one works by summing up values in the left line and subtract the sum of the right line.
    /// Calculation is made per color.
    /// o   o
    /// o   o
    /// o   o
    /// o x o
    /// o   o
    /// o   o
    /// o   o
    /// </summary>
    public class EdgeDetectVerticalFilter : Conv3x3
    {
        [ParameterAttribute(Name = "Edge Detection Type", DefaultValue = (int)EdgeDetectionType.EDGE_DETECT_VERT)]
        public EdgeDetectionType DetectionType { get { return EdgeDetectionType.EDGE_DETECT_VERT; } }

        protected override void PrepareData(DotNetImage bmp)
        {
            base.PrepareData(bmp);
            bmp.CreatePixelCopy();
            startX = 1;
            startY = 3; // Skip 3 top and bottom pixels
        }

        protected override void ProcessPixel(ref Pixel pix, uint x = 0, uint y = 0)
        {
            Pixel pixLeftLine = Subject.GetPreservedPixel(x - 1, y - 3)
                + Subject.GetPreservedPixel(x - 1, y - 2)
                + Subject.GetPreservedPixel(x - 1, y - 1)
                + Subject.GetPreservedPixel(x - 1, y - 0)
                + Subject.GetPreservedPixel(x - 1, y + 1)
                + Subject.GetPreservedPixel(x - 1, y + 2)
                + Subject.GetPreservedPixel(x - 1, y + 3);

            Pixel pixRightLine = Subject.GetPreservedPixel(x + 1, y - 3)
                + Subject.GetPreservedPixel(x + 1, y - 2)
                + Subject.GetPreservedPixel(x + 1, y - 1)
                + Subject.GetPreservedPixel(x + 1, y - 0)
                + Subject.GetPreservedPixel(x + 1, y + 1)
                + Subject.GetPreservedPixel(x + 1, y + 2)
                + Subject.GetPreservedPixel(x + 1, y + 3);

            pixLeftLine.Subtract(pixRightLine);
            pix = pixLeftLine;
        }
    }
}
